#include "Vertex.h"
#include "Face.h"


/************************************************
/*				Public
/************************************************/

    /************************************************
    /*				Contructors
    /************************************************/

        Vertex::Vertex(){

        }

    /************************************************
    /*				Methods
    /************************************************/

        vector<AttributeTemplate*>& Vertex::Attributes(){
            return vertexTemplate->attributes;
        }

        void Vertex::SetTemplate(VertexTemplate * vt){
            vertexTemplate=vt;
            size=vertexTemplate->totalSize;
            attributeCount=Attributes().size();
        }

        bool Vertex::SetAttribute(string name, float x){
            return SetAttribute(name,x,0,0,0);
        }

        bool Vertex::SetAttribute(string name, float x, float y){
            return SetAttribute(name,x,y,0,0);
        }

        bool Vertex::SetAttribute(string name, float x, float y, float z){
            return SetAttribute(name,x,y,z,0);
        }

        bool Vertex::SetAttribute(string name, float x, float y, float z, float w){
            bool found=false;
            int i=0;
            while(!found && i < vertexTemplate->attributes.size()){
                if(vertexTemplate->attributes[i]->name==name){

                    s_Vertices.at(offset+  Attributes().at(i)->offset)=x;

                    if(Attributes().at(i)->size>1){
                        s_Vertices.at(offset+ Attributes().at(i)->offset+1)=y;
                    }
                    if(Attributes().at(i)->size>2){
                        s_Vertices.at(offset+ Attributes().at(i)->offset+2)=z;
                    }
                    if(Attributes().at(i)->size>3){
                        s_Vertices.at(offset+ Attributes().at(i)->offset+3)=w;
                    }
                    found=true;
                }
                i++;
            }
            return found;
        }

        bool Vertex::SetAttribute(int pos, float x){
            return SetAttribute(pos,x,0,0,0);
        }

        bool Vertex::SetAttribute(int pos, float x, float y){
            return SetAttribute(pos,x,0,0,0);
        }

        bool Vertex::SetAttribute(int pos, float x, float y, float z){
            return SetAttribute(pos,x,0,0,0);
        }

        bool Vertex::SetAttribute(int pos, float x, float y, float z, float w){
            if(pos> Attributes().size()){

                s_Vertices.at(offset+ Attributes().at(pos)->offset)=x;

                if(Attributes().at(pos)->size>1){
                    s_Vertices.at(offset+ Attributes().at(pos)->offset+1)=y;
                }
                if(Attributes().at(pos)->size>2){
                    s_Vertices.at(offset+ Attributes().at(pos)->offset+2)=z;
                }
                if(Attributes().at(pos)->size>3){
                    s_Vertices.at(offset+ Attributes().at(pos)->offset+3)=w;
                }
            }
            return false;

        }

/************************************************/
/*				Statics 						*/
/************************************************/

    /************************************************/
    /*				Properties 						*/
    /************************************************/

     vector<GLfloat>  Vertex::s_Vertices;


    /************************************************/
    /*				Methods 						*/
    /************************************************/


    void Vertex::AddVertex(Vertex *vertex){
        int     size    =vertex->vertexTemplate->totalSize;

        float * data = new float[size];

        for(int i=0; i< size; i++){
            data[i]=0.0f;
        }

        if(Vertex::s_Vertices.size()< vertex->offset){
            //s_Vertices.insert(vertex->offset, size, data);
        }
        else{
           // s_Vertices.push_back(data);
        }
    }



/************************************************/
/*				Members 						*/
/************************************************/


    float* Vertex::Array(){
        return &s_Vertices[0];
    }

    void Vertex::Print(){
        for(int i=0; i< s_Vertices.size();i++){
            if(i%4==0){
                printf("\n");
            }
            printf("%f \t", s_Vertices.at(i));
        }
    }

    void Vertex::InsertVertexBuffer(){

        if(s_Vertices.size()==offset){
            for(int i=0; i< size; i++){
                s_Vertices.push_back(0.0f);
            }
        }else{
            for(int i=0; i< size; i++){
                s_Vertices.insert(s_Vertices.begin()+offset+i,  0.0f);
            }
        }
    }

    void Vertex::ComputeNormal()
    {
        vec4 sum;

        /*for(int i=0;i<m_Faces.size();i++)
        {
            sum+=m_Faces.at(i)->m_Normal;
        }

        sum= sum/m_Faces.size();
 //       m_Normal = (sum).Normalize();*/
    }

    /************************************************/
    /*				Constructors					*/
    /************************************************/


